// PlayerView.ts
import { _decorator, Label, EditBox } from 'cc';
import { ModelView } from './ModelView';
import { BindingInfo } from './DataBinder';
const { ccclass, property } = _decorator;

interface Player {
    hp: number;
    name: string;
}

@ccclass('PlayerView')
export class PlayerView extends ModelView<Player> {

    @property(Label)
    hpLabel: Label = null!;

    @property(EditBox)
    nameInput: EditBox = null!;

    start() {

        // 定义绑定：字段 → (组件, 属性, 事件)
        const bindings: Record<keyof Player, BindingInfo> = {
            hp: {
                node: this.hpLabel,
                viewProp: 'string', // cc.Label.string
            },
            name: {
                node: this.nameInput,
                viewProp: 'string', // cc.EditBox.string
                eventName: 'text-changed',
            },
        };

        // 初始化 ModelView
        this.bindModel({ hp: 100, name: 'Hero' }, bindings);

        // 测试：3 秒后修改 Model，会自动更新 hpLabel
        this.scheduleOnce(() => {
            this.model.hp = 50;
        }, 3);
    }
}
